/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package issr.boundary;

import com.vaadin.data.Item;
import com.vaadin.ui.Table;
import com.vaadin.ui.VerticalLayout;
import issr.boundary.vaadin.AbstractView;
import issr.controller.ControlloreLogAmministratoreG;
import issr.controller.ControlloreLogAmministratoreG_Service;
import issr.controller.Log;
import java.util.Date;
import java.util.List;
import javax.xml.datatype.XMLGregorianCalendar;

/**
 *
 * @author Soulreaver
 */
public class PannelloLogAmministratoreG extends AbstractView {

    private ControlloreLogAmministratoreG_Service cls;
    private ControlloreLogAmministratoreG controllore;
    private final Table textTable;
    private final VerticalLayout layout;

    public PannelloLogAmministratoreG() {
        cls = new ControlloreLogAmministratoreG_Service();
        controllore = cls.getControlloreLogAmministratoreGPort();
        layout = new VerticalLayout();
        textTable = new Table();
        textTable.addContainerProperty("Timestamp", XMLGregorianCalendar.class, null);
        textTable.addContainerProperty("Attività", String.class, null);
        textTable.addContainerProperty("Utente", String.class, null);
        textTable.addContainerProperty("Punto vendita", String.class, null);
        // Create the visual components of the view.
        setSizeFull();
        setCompositionRoot(layout);

        layout.setHeight("100%");
        layout.setWidth("100%");
        layout.setSpacing(true);
        layout.setMargin(true);
    }

    public void visualizzaLog() {
        List<Log> entries = controllore.ottieniInteroLog();
        for (int dummyId = 0; dummyId < entries.size(); dummyId++) {
            Item item = textTable.addItem(dummyId);
            item.getItemProperty("Timestamp").setValue(entries.get(dummyId).getEntrytime());
            item.getItemProperty("Attività").setValue(entries.get(dummyId).getEntry());
            item.getItemProperty("Utente").setValue(entries.get(dummyId).getGetUtente().getLogin());
            if (entries.get(dummyId).getPuntovendita() != null) {
                item.getItemProperty("Punto vendita").setValue(entries.get(dummyId).getPuntovendita().getNome());
            } else {
                item.getItemProperty("Punto vendita").setValue("");
            }
        }
        textTable.sort(new Object[]{"Timestamp"}, new boolean[]{false});
        
        textTable.setSizeFull();
        textTable.setPageLength(30);
        textTable.setEditable(false);
        textTable.setImmediate(true);
        layout.addComponent(textTable);
    }

    @Override
    protected void close() throws Exception {
    }

    @Override
    protected void open() throws Exception {
        textTable.removeAllItems();
        visualizzaLog();
    }
}
